/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-9
 */
package com.guidecaptain.common.validatecode.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.guidecaptain.common.validatecode.model.ValidateCode;
import com.guidecaptain.common.validatecode.service.ValidateImageManager;
import com.guidecaptain.core.base.action.BaseAction;
import com.guidecaptain.core.common.CookieManager;

/**
 * 图形验证码 Action
 * 
 * @author fonfon
 */
public class ValidateCodeAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 用于保存已经生成的图形验证码输入流对象，以便在前台显示图形
	 */
	private ByteArrayInputStream inputStream;

	/**
	 * 图形验证码创建接口
	 */
	@Autowired
	private ValidateImageManager validateImageManager;

	private ValidateCode vc = new ValidateCode();
	
	/**
	 * 生成验证码
	 * 
	 * <img src="http://xxxxxxxxx:xxxx/validatecode/validate!execute.jspa" />
	 * 
	 */
	public String execute() throws Exception {
		CookieManager cookie = new CookieManager(this.getRequest(), this.getResponse());
		String code=createInputStream(ValidateImageManager.DISTURB_TYPE_DEFAULT);
		cookie.addCookie("validateCode", code);
		return SUCCESS;
	}
	
	/**
	 * 检查验证码是否正确
	 * 
	 * http://xxxxxxxxx:xxxx/validatecode/validate!checkCode.jspa?code=xxxxxx
	 * 
	 * @return
	 */
	public String checkCode() {
		CookieManager cookie = new CookieManager(this.getRequest(), this.getResponse());
		String code = cookie.getCookieValue("validateCode");
		JSONObject jsonObject=new JSONObject();
		if (StringUtils.isBlank(code) || !code.equals(vc.getCode())) {
			jsonObject.put("state", false);
		} else {
			jsonObject.put("state", true);
		}
		setJsonObject(jsonObject);
		
		return JSON;
	}
	
	/**
	 * 在Action层创建图形验证码，并将创建好的图形字节流保存到inputStream对象中，并返回验证码
	 * @param disturbType 绘制干扰线的类型
	 *                    0：不绘制干扰线
	 *                    1：绘制单一色调的干扰线
	 *                    2：绘制复杂的干扰线
	 * @return 返回已经生成好的验证码字符
	 * @throws IOException 
	 */
	private String createInputStream(int disturbType) throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		String validateCode = validateImageManager.createValidateCode(disturbType, 25, bos, 95, 22, getText("System.validateCode", ValidateImageManager.DEFAULT_VALIDATE_CODE), 4);  
		
		inputStream = new ByteArrayInputStream(bos.toByteArray());
		
		bos.close();
		
		return validateCode;
	}
	
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}	

	public ValidateCode getVc() {
		return vc;
	}

	public void setVc(ValidateCode vc) {
		this.vc = vc;
	}

	public Object getModel() {
		
		return vc;
	}
}